package com.taptech.doufu.services;

import android.graphics.Bitmap;

import com.taptech.doufu.sdk.qiniu.resumableio.PutExtra;
import com.taptech.doufu.sdk.qiniu.service.QiniuService;
import com.taptech.doufu.ui.activity.base.DiaobaoBaseActivity;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.listener.HttpResponseListener;
import com.taptech.doufu.listener.UGCJSONObjectRet;
import com.taptech.doufu.ui.activity.AddTagActivity;
import com.taptech.doufu.util.DiaobaoUtil.FileType;
import com.taptech.doufu.util.FileHashUtil;
import com.taptech.doufu.net.httputils.HttpRequestObject;
import com.taptech.doufu.net.httputils.HttpResponseObject;
import com.taptech.doufu.net.httputils.HttpUtil;
import com.taptech.doufu.ui.view.ImageUtil;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import java.util.LinkedList;
import java.util.List;

public class UGCMainService implements HttpResponseListener {

    public static final String BASE_URL = "http://" + Constant.HOST_NAME + "/index.php/post/";


    private static UGCMainService instance = new UGCMainService();

    public static final int HANDLE_TYPE_PRE_UPLOAD = 3002;
    public static final int HANDLE_TYPE_LOAD_SHARE_SCORE = 3009;
    public static final int HANDLE_LOAD_TAGS_INFO = 3010;
    public static final int HANDLE_TYPE_LOAD_SEARCH_TAG = 3015;


    private UGCMainService() {

    }

    public static UGCMainService getInstance() {
        return instance;
    }


    /**
     * 分享统计
     */
    public void uploadShareScore(String id, String social_type, String object_type, HttpResponseListener listener) {
        HttpRequestObject request = new HttpRequestObject();
        request.setHandleType(HANDLE_TYPE_LOAD_SHARE_SCORE);
        List<NameValuePair> lsit = new LinkedList<NameValuePair>();
        NameValuePair value;
        value = new BasicNameValuePair("id", id);
        lsit.add(value);
        value = new BasicNameValuePair(Constant.SOCIAL_TYPE, social_type);
        lsit.add(value);
        value = new BasicNameValuePair(Constant.OBJECT_TYPE, object_type);
        lsit.add(value);
        request.setUrl(Constant.BASE_URL + "stat/share");
        request.setListener(listener);
        request.setData(lsit);
        HttpUtil.sendPostRequest(request);

    }


    public void getQiniuToken(HttpResponseListener listener, byte[] datas) {
        try {
            StringBuffer sb = new StringBuffer();
            String path = FileHashUtil.getDiabaoFileSHA384(datas, FileType.PNG);
            sb.append("path[").append(0).append("]=").append(path);

            HttpRequestObject request = new HttpRequestObject();
            request.setHandleType(HANDLE_TYPE_PRE_UPLOAD);
            request.setUrl(Constant.BASE_URL + "post/check_files?" + sb.toString());
            request.setListener(listener);
            HttpUtil.sendGetRequest(request);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void getQiniuToken(HttpResponseListener listener, Bitmap bitmap) {
        try {
            StringBuffer sb = new StringBuffer();
            byte[] datas = ImageUtil.Bitmap2Bytes(bitmap);
            String path = FileHashUtil.getDiabaoFileSHA384(datas, FileType.PNG);
            sb.append("path[").append(0).append("]=").append(path);

            HttpRequestObject request = new HttpRequestObject();
            request.setHandleType(HANDLE_TYPE_PRE_UPLOAD);
            request.setUrl(Constant.BASE_URL + "post/check_files?" + sb.toString());
            request.setListener(listener);
            HttpUtil.sendGetRequest(request);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void uploadQiniu(String fileName, byte[] datas, PutExtra extra, UGCJSONObjectRet ret) {
        try {
            QiniuService.getInstance().upload(fileName, datas, extra, ret);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 添加点赞
     */
    public void addPraiseUser(String object_id, HttpResponseListener listener) {
        HttpRequestObject request = new HttpRequestObject();
        request.setUrl(Constant.BASE_URL + "post/add_praise?object_id=" + object_id);
        request.setListener(listener);
        HttpUtil.sendGetRequest(request);
    }

    /**
     * 取消点赞
     */
    public void delPraiseUser(String object_id, HttpResponseListener listener) {
        HttpRequestObject request = new HttpRequestObject();
        request.setUrl(Constant.BASE_URL + "post/del_praise?object_id=" + object_id);
        request.setListener(listener);
        HttpUtil.sendGetRequest(request);
    }

    public void loadTags_byType(int object_type, HttpResponseListener listener) {
        HttpRequestObject request = new HttpRequestObject();
        request.setHandleType(HANDLE_LOAD_TAGS_INFO);
        request.setUrl(Constant.BASE_URL + "journals/default_tag?object_type=" + object_type);
        if (listener != null) {
            request.setListener(listener);
        } else {
            request.setListener(this);
        }
        if (listener instanceof AddTagActivity) {
            HttpUtil.sendLockedGetRequest(request, (DiaobaoBaseActivity) listener);
        } else {
            HttpUtil.sendGetRequest(request);
        }

    }


    @Override
    public void handleResponse(int handleType, HttpResponseObject response) {
        // TODO Auto-generated method stub

    }


    public void getSearchTags(String name, String last, HttpResponseListener listener) {
        HttpRequestObject request = new HttpRequestObject();
        request.setHandleType(HANDLE_TYPE_LOAD_SEARCH_TAG);
        String url = Constant.BASE_URL + "tags/search?" + "name=" + name + "&last=" + last + "&size=" + Constant.PAGE_SIZE_20;
        request.setUrl(url);
        request.setListener(listener);
        HttpUtil.sendGetRequest(request);
    }

}
